# 蓝色框为桥梁混凝土和钢筋的参数，由具体桥梁决定，此处为假设值。程序设置时，我们需手动输入。
# 黄色框为通过计算所得的。
# 绿色框为固定值。
# 橘色框为计算机裂缝识别所得参数。
from userModel import useModel


def straight(b, h, ks, As, fc, ft, ec, fy, es, l, w1, w2):
    # 基于正裂缝信息的混凝土简支梁受弯状态评估
    # 宽度b（mm） - b
    # 高度h(mm) - h
    # 钢筋合力作用点到边缘的距离δs（mm） - ks
    # 钢筋截面积As(mm^2) - as
    # 混凝土抗压强度fc (Mpa) - fc
    # 混凝土抗拉强度ft(Mpa) - ft
    # 混凝土弹性模量Ec(Mpa) - ec
    # 钢筋屈服fy(Mpa) - fy
    # 钢筋弹性模量Es(Mpa) - es
    # 裂缝间距l(mm)

    # 裂缝宽度w1(mm)
    # 裂缝宽度w2
    try:
        h0 = h - ks
        eps = 2 * ft / ec
        n = es / ec
        p = As / (b * h0)

        x = (2 * n * p * pow(h0, 2) + pow(h, 2)) / (2 * n * p * h0 + 2 * h)
        seg_s = es * ((w1 + w2) / 2 + l * (eps * (h0 - x) / (h - x))) / l

        M = seg_s * As * h0 * (1 - 0.84 * seg_s * As / (b * fc * h0))
        Mcr = 2 * es / ec * As * ft * ((h0 - x) * (h0 - x / 3) / (h - x)) + ft * b * (h - x) * (h / 2 + x / 6)
        Mu = abs(fy * As * (h0 - 0.197 * x) - 0.1714 * 32.4 * b * pow(x, 2))
        return h0, eps, n, p, x, seg_s, M, Mcr, Mu
    except:
        return ["错误的参数值\n"]


def outputStraight(b=15000, h=2000, ks=30, As=58890, fc=32.4, ft=2.65, ec=34500, fy=235, es=200000, l=4643):
    positive, negative = useModel(1, "predict/")
    result = ""
    for i in negative:
        # print("Negative: " + i)
        result += "Negative: " + i + "\n"
    for i in positive:
        # print("-"*50)
        result += "-" * 50 + "\n"
        # print(i[0] + " 面积:" + str(i[1]) + " 长度:" + str(i[2]) + " 宽度:" + str(i[3]) + " 斜度(弧度制):" + str(i[4]))
        result += i[0] + " 面积:" + str(i[1]) + " 长度:" + str(i[2]) + " 宽度:" + str(i[3]) + " 斜度(弧度制):" + str(i[4]) + "\n"
        # print("-"*50)
        result += "-" * 50 + "\n"

    if len(positive) < 2:
        # print("裂缝数量不足")
        result += "裂缝数量不足" + "\n"
    else:
        w1 = positive[0][3]
        w2 = positive[1][3]
        # print("演示数据为：%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f"%(b, h, ks, As, fc, ft, ec, fy, es, l,w1,w2))
        result += "演示数据为：%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f" % (b, h, ks, As, fc, ft, ec, fy, es, l, w1, w2) + "\n"
        straight_eg = straight(b, h, ks, As, fc, ft, ec, fy, es, l, w1, w2)
        if len(straight_eg) == 1:
            return straight_eg[0]
        M = straight_eg[6]
        Mcr = straight_eg[7]
        Mu = straight_eg[8]

        # print("-"*50)
        result += "-" * 50 + "\n"
        # print("l-1截面弯矩M(KN*m):\t",M)
        result += "l-1截面弯矩M(KN*m):\t" + str(M) + "\n"
        # print("截面开裂弯矩Mcr(KN*m):\t",Mcr)
        result += "截面开裂弯矩Mcr(KN*m):\t" + str(Mcr) + "\n"
        # print("极限承载力Mu(KN*m):\t",Mu)
        result += "极限承载力Mu(KN*m):\t" + str(Mu) + "\n"
        # print("-"*50)
        result += "-" * 50 + "\n"
        # print("评估结果：")
        result += "评估结果：" + "\n"
        if (M <= Mcr):
            result += "弯矩M不大于开裂弯矩Mcr，梁未开裂." + "\n"
        elif (M > Mcr and M <= Mu):
            result += "弯矩M大于开裂弯矩Mcr但不大于极限承载力Mu，梁开裂但未达到极限承载力." + "\n"
        elif (M > Mu):
            result += "弯矩M大于大于极限承载力Mu，梁已破坏." + "\n"
        # print("-"*50)
        result += "-" * 50 + "\n\n"
        return result
